<?php
class Reg extends CI_Controller {
	public function __construct() {
		parent::__construct ();
		$this->load->helper ( array (
				'form',
				'url' 
		) );
	}
	
	/**
	 * 注册页面显示
	 */
	public function index() {
		$this->load->helper ( 'form' );
		$this->load->view ( 'mobile/reg' );
	}
	
	/**
	 * 提交数据
	 */
	public function info_post() {
		if (empty ( $_POST ['mobile'] )) {
			
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		if (empty ( $_SESSION ['mobile'] )) {
			
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		if (empty ( $_SESSION ['code'] )) {
			
			exit ( $this->common->alert_msg_goback ( '参数错误' ) );
		}
		if($_POST ['mobile'] != $_SESSION['mobile'])
		{
			exit ( $this->common->alert_msg_goback ( '请输入正确的手机号' ) );
		}

		if($_POST ['check_code'] != $_SESSION['code'])
		{
			exit ( $this->common->alert_msg_goback ( '请输入正确的手机号' ) );
		}

		if($_POST['up_account'] != 'dedaozhe')
		{
			$up_user = $this->user->get_uesr_info($_POST['up_account']);

			if(empty($up_user) || $up_user['kick_status'] == 1)
			{
				exit ( $this->common->alert_msg_goback ( '推广账号不存在' ) );
			}

			$pid = $up_user['id'];

			$path = $up_user['path'];
		}
		else
		{
			$pid = 0;

			$path = '0/';
		}


		
		$mobile = $this->user->get_user_mobile($_POST['mobile']);

		if(!empty($mobile))
		{
			exit ( $this->common->alert_msg_goback ( '每个手机号只能注册一次' ) );
		}

		$info = $this->user->get_uesr_info ( $_POST ['user_account'] ); 
		
		if (! empty ( $info )) {
			
			exit ( $this->common->alert_msg_goback ( '公益账号已经存在' ) );
		} else {
			// 插入
			$result = $this->user->user_insert ($pid);
			
			if ($result) {

				$this->user->update_path($result,$path);

				$this->user->reg_digui_up_account($_POST['up_account']);
				
				exit ( $this->common->alert_msg_url ( '提交成功', site_url ( 'mobile/login' ) ) );
			} else {
				
				exit ( $this->common->alert_msg_goback ( '提交失败' ) );
			}
		}
	}
	
	/**
	 * 发送校验码
	 */
	public function ajax_send_check_code() {
		if (empty ( $_POST ['mobile'] )) {
			$res ['err'] = - 1;
			$res ['msg'] = '手机号不能为空';
			$this->common->ajax_return ( $res );
		}
		
		$mobile = $_POST ['mobile'];
		
		$r = rand ( 100000, 999999 );
		
		$content = "您的验证码为：{$r}，请不要把验证码泄露给其他人。";
		$result = $this->common->send_sms ( $mobile, $content );
		
		if ($result ['SubmitResult'] ['code'] == 2) {
			$_SESSION ['code'] = $r;
			
			$_SESSION ['mobile'] = $mobile;
			
			$res ['err'] = 1;
			
			$res ['code'] = md5 ( $r );
		} else {
			$res ['err'] = - 2;
			$res ['result'] = $result;
			$res ['msg'] = '提交失败';
		}
		$this->common->ajax_return ( $res );
	}
}
